﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ATW.MT.BUS;
using System.Data;
using System.Text;
using BUS;
using DAL.Dao;

namespace ATW.MT.SellManage
{
    public partial class _addExecption : System.Web.UI.Page
    {
        bus_T_Exception TE = new bus_T_Exception();
        CommonHelp CH = new CommonHelp();
        MessageDao _MessageDao = new MessageDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserInfo"] == null)
            {
                Response.Redirect("../Main/Login.aspx");
            }
        }
        protected void lbnSave_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["cid"]))
            {
                string content = txtExContent.Text.Trim();
                string contractID = Request.QueryString["cid"];
                if (content == "")
                {
                    Tips("内容不能为空！");
                    return;
                }
                if (!CommonClass.common.filterChar(content)
                    || !CommonClass.common.filterChar(contractID))
                {
                    Tips("内容含有危险字符！");
                    return;
                }
                try
                {
                    int result = TE.addException(contractID, content, "1");
                    if (result > 0)
                    {
                        Tips("登记成功！");
                        //调用发送信息方法
                        string uid = TE.getContractUser(contractID);
                        pushException(contractID, uid);
                        CloseWindowReferesh();
                    }
                    else
                    {
                        Tips("登记失败！");
                    }
                }
                catch (Exception ex)
                {

                }
            }

        }
        private void pushException(string contractid, string uid)
        {//推送合同异常
            try
            {
                #region 多语言
                string strTip1 = "";
                string strTip2 = "";
                string requestID = TE.getRequestID(contractid);
                string strCountryLan = _MessageDao.GetUserCountryLan(requestID);

                strTip1 = _MessageDao.GetTips("24", strCountryLan);
                strTip2 = _MessageDao.GetTips("25", strCountryLan);

                #endregion


                Dictionary<string, string> Parm = new Dictionary<string, string>();
                Parm.Add("ContractId", contractid);
                Parm.Add("Type", "1");
                T_Message Entry = new T_Message();
                Entry.tmType = "2";
                Entry.tmfrom = "system";
                Entry.tmTitle = strTip1;
                Entry.tmto = uid;
                Entry.tmMessage = strTip2;

                Entry.toType = "3";
                Entry.toId = contractid;

                CH.SendMessage(Entry, "3", Parm);
            }
            catch (Exception ex)
            {

            }
        }
        private void Tips(string s)
        {//提示
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
        private void CloseWindowReferesh()
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                if(window.opener)
                {
                    window.opener.location.href=window.opener.location.href;
                    window.close();
                }
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "Close", strScript.ToString());
        }
    }
}